﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using SmartMathLibrary;
using SmartMathLibrary.Management;
using SmartMathLibrary.Generator;
using SmartMathLibrary.Management.Monitoring;
using SmartMathLibrary.NonlinearEquationSolvers;
using SmartMathLibrary.LinearEquationSolvers;
using SmartMathLibrary.NumericalIntegration;
using SmartMathLibrary.Optimization;
using SmartMathLibrary.Blas.Engine;
using SmartMathLibrary.FFTPack;
using SmartMathLibrary.OdePack;
using SmartMathLibrary.PolynomialPack;
using SmartMathLibrary.Space;
using SmartMathLibrary.Space.CoreTypes;
using SmartMathLibrary.Space.QueryEngine;
using SmartMathLibrary.Statistics.RandomNumberGenerator;

namespace QuickTest
{
    class Program
    {
        public static ComplexNumber comFunc(ComplexNumber x)
        {
           return  ExtendedMath.Pow(x, new ComplexNumber(1,2));
        }

        public static double _ROF(double x)
        {
              
            
           
            //return 6 * x - 5 * Math.Cos(x);
            //-6x^5 - 5x^4 - x^3 - 8x

            return -6 * Math.Pow(x, 5) - 5 * Math.Pow(x, 4) -   Math.Pow(x, 3) - 8*x+10;
        }

        public static double __FU(GeneralVector x)
        {
            //return 6 * x - 5 * Math.Cos(x);

            return 3 * Math.Pow(x[0], 2) + x[0] * x[1] - Math.Pow(x[1], 2) - 4 * x[0] - 5 * x[1] + 6;
        }

        public static ComplexNumber eCFU(ComplexNumber x)
        {
            return x * 6 - x * 5;
        }

        public static double FU1(GeneralVector x)
        {
            return x[0] + x[1] - 1;
            //return Math.Cos(2.0 * x[0]) - Math.Cos(2.0 * x[1]) - 0.4;
        }

        public static double FU4(GeneralVector x)
        {
            return (Math.Pow(x[6], 5) +   Math.Pow(x[5], 4) + Math.Pow(x[4], 3) + Math.Pow(x[3], 2) + Math.Pow(x[2], 1) +
                   x[1] )* x[0]+1000;
            //return Math.Cos(2.0 * x[0]) - Math.Cos(2.0 * x[1]) - 0.4;
        }

        public static double FU3(GeneralVector x)
        {
            return Math.Pow(x[0], 3) * 5 + 8 * x[1];
        }


        public static double FU2(GeneralVector x)
        {
            return 2.0 * (x[1] - x[0]) + Math.Sin(2.0 * x[1]) - Math.Sin(2.0 * x[0]) - 1.2;
        }

        public static double FU5(GeneralVector x)
        {
            return((2000.0 * (x[1] - x[0]+30) + Math.Sin(2.0 * x[1]) - Math.Sin(2.0 * x[0]) - 1.2))*Math.Sqrt(x[2])-1000;
        }


        public static ComplexNumber CCFU2(GeneralComplexVector x)
        {
            return (x[1] - x[0])*2 +   x[1]  *2-   x[0] - 1.2;
        }

        public static ComplexNumber CCFU1(GeneralComplexVector x)
        {
            return (x[1] + x[0]) * 1.4 - x[1] * 8+x[0] - 5;
        }

        public static GeneralVector CURVEFITTER(GeneralVector  x, GeneralVector param)
        {
             GeneralVector fv = new GeneralVector(x.Count);
             
            for (int i = 0; i < x.Count; i++)
             {
                 fv[i] = param[0] * Math.Pow(x[i], 2) + param[1]*x[i];
             }

            
            //double xp;
            //GeneralVector fv = new GeneralVector(x.Count);
            //for (int i = 0; i < x.Count; i++)
            //{
            //    xp = param[2] * x[i] - param[1];
            //    fv[i] = param[0] / (1.0 + xp * xp);
            //}

            return fv;
        }

        public static double xHoch3(double x)
        {
            return Math.Pow(x, 4);
        }

        public  static void Reftest(ref int n, ref double s, ref double[] x, ref int intx)
        {
            unsafe
            {
                fixed (double* bt = x)
                {
                    fixed (int* n1 = &n)
                    {
                        fixed (double* s1 = &s)
                        {
                            fixed (int* inx1 = &intx)
                            {
                                ManagedBlasProvider.dscal_(n1, s1, bt, inx1);
                                //n = *n1;
                            }
                        }
                    }

                }
            }

        }

        static void Main(string[] args)
        {
            //SpaceClientProvider provider = new SpaceClientProvider("localhost", "space", 1239);

            //provider.Connect();

            //SpaceContainer con99 = new SpaceContainer();



            //uint[] r = con99.SearchByTemplate(new SpaceVector(new double[] { 1, 2, 3 }));

            //Console.WriteLine("sr: " + r[0]);
            //*******************************************

            SpaceContainer con = new SpaceContainer();

            //con.Add(new SpaceItem(con.GetFreeID(), new SpaceIntegral(1, 4, new Polynomial(new double[] { 1, 2, 3 }))));
            //con.Add(new SpaceItem(con.GetFreeID(), new SpaceDouble(10)));
            //con.Add(new SpaceItem(con.GetFreeID(), new SpaceDouble(2)));
            //con.Add(new SpaceItem(con.GetFreeID(), new SpaceDouble(3)));
            //con.Add(new SpaceItem(con.GetFreeID(), new SpaceDouble(4)));
            //con.Add(new SpaceItem(con.GetFreeID(), new SpaceDouble(10)));
            //con.Add(new SpaceItem(con.GetFreeID(), new SpaceDouble(10)));

            con.Add(new SpaceItem(con.GetFreeID(), new SpaceIntegral(1,5,new Polynomial(1))));
            con.Add(new SpaceItem(con.GetFreeID(), new SpaceIntegral(6, 15, new Polynomial(1))));
            con.Add(new SpaceItem(con.GetFreeID(), new SpaceIntegral(0, 4, new Polynomial(1))));
            con.Add(new SpaceItem(con.GetFreeID(), new SpaceString("Hallo")));
            con.Add(new SpaceItem(con.GetFreeID(), new SpaceString("mull")),2);

       
            Console.WriteLine(((SpaceString) con.RequestSpaceObject(4).Value).Data);
            Thread.Sleep(1000*60);
            con.Pushback(4);
            //uint[] ee = con.ExecuteQuery(new SubStringQuery("Hallo",StringComparison.Ordinal));

            //foreach (var u in ee)
            //{
            //    Console.WriteLine("ee@ " + u);
            //}

            
           

            Thread.Sleep(3 * 1000 * 60);
            Console.WriteLine(((SpaceString)con.RequestSpaceObject(4).Value).Data);
            con.Pushback(4);
            //*******************************************
            //Console.WriteLine(ExtendedMath.Remainder(20,6));
            //Console.WriteLine(ExtendedMath.ModularMultiplicativeInverse(1234,9997));
            ////HardComplexFunction hr = new HardComplexFunction(comFunc);
            //ComplexFinitelyIntegral intg = new ComplexFinitelyIntegral(new ComplexNumber(1, 2), new ComplexNumber(5, 8),hr);
            //ComplexExponentialTransformIntegrator ita = new ComplexExponentialTransformIntegrator(intg);

            //Console.WriteLine(ita.Integrate(10));
            //ComplexNumber s = new ComplexNumber(52,-10);
            //Console.WriteLine(s.Argument);
            //Console.WriteLine(Math.Atan2(s.ImaginaryNumberPart, s.RealNumberPart));
          //  Console.WriteLine(  9*s);

            //ZigguratREXPRandomGenerator rrr = new ZigguratREXPRandomGenerator();

            //for (int _a = 0; _a < 20;_a++)
            //    Console.WriteLine(rrr.Next());

            //HardRealFunction _z = new HardRealFunction(new HardRealFunctionPointer(xHoch3));

            //FinitelyDoubleIntegral _i = new FinitelyDoubleIntegral(-1,-5,_z);

            //LeftPointIntegrator integrator1 = new LeftPointIntegrator(-5,1,_z);
            //RightPointIntegrator integrator2 = new RightPointIntegrator(-5, 1, _z);
            //MidPointIntegrator integrator3 = new MidPointIntegrator(-5, 1, _z);
            //TrapezoidCompositeIntegrator integrator4 = new TrapezoidCompositeIntegrator(-5, 1, _z);
            //SimpsonCompositeIntegrator integrator5 = new SimpsonCompositeIntegrator(-5, 1, _z);
            //RombergIntegrator integrator6 = new RombergIntegrator(-5, 1, _z);

            //Console.WriteLine(integrator1.Integrate(100,1e-5));
            //Console.WriteLine(integrator2.Integrate(100, 1e-5));
            //Console.WriteLine(integrator3.Integrate(100, 1e-5));
            //Console.WriteLine(integrator4.Integrate(100, 1e-5));
            //Console.WriteLine(integrator5.Integrate(100, 1e-5));
            //Console.WriteLine(integrator6.Integrate(100, 1e-5));

            //Console.ReadKey();
            ////LuaTestClass t = new LuaTestClass();

            ////t.DO();
            //// Console.ReadKey();


            //double[] A = new double[] { 1, 2, 3 };
            //double[] B = new double[] { 1, 2, 3 };
            //int n = 3;
            //double s = 2;
            // int inc = 2;

            //Reftest(ref n, ref s, ref A, ref inc);

            //foreach (double d in A)
            //{
            //    Console.WriteLine(d);
            //}

            //Console.ReadKey();


            //ExtendedRandom ri = new ExtendedRandom();
            //Polynomial r = ri.NextPolynomial(8);

           



//            //
//// 'x' coordinates of measured data
////
////double[] x  = {
////    0.0,
////    1.1,
////    2.2,
////    3.3,
////    4.4,
////    5.5,
////    6.6};

//            double[] x = {
//    -8.24,
//   -5.84,
//    -3.95,
//   -2.74,
//   3.45};

////
//// 'y' coordinates of measured data
////
////double[] y = {
////    0.1,
////    0.21,
////    0.5,
////    1.05,
////    0.5,
////    0.3,
////    0.08};

//            double[] y = {
//    4.02,
//    1.76,
//     3.03,
//   9.14,
//    -6.24
//   };

//            GeneralVector param = new GeneralVector(2);

//            param[0] = 1.2;         // initialize estimates for parameter values
//            param[1] = 2.2;
//            //param[2] = 0.9;


//            CurveFitting fitting = new CurveFitting(CURVEFITTER);

//            GeneralVector mmm= fitting.FitCurve(new GeneralVector(x), new GeneralVector(y), param, new CurveFittingParameter[] {0, 0, 0},  100,1e-5);

//          for (int hh =0; hh < mmm.Count;hh++)
//              Console.WriteLine(mmm[hh]);

//            Console.ReadKey();

          //  MultivariantNewtonRaphsonRootFinder muva = new MultivariantNewtonRaphsonRootFinder(new HardMultivariateRealFunctionPointer(FU4));

            //ExtendedRandom rado = new ExtendedRandom();


            //Console.WriteLine(muva.FindRoots(new GeneralVector(new double[]{1,2,3 ,4,5,6,7}),1e-5,100 ));

            //GeneralVector gena = muva.FindRoots(new GeneralVector(new double[] { 1, 2, 3, 4, 5, 6, 7 }), 1e-5, 100);

            //Console.WriteLine(FU4(gena));
            //Console.WriteLine(muva.NeededIterations);
            //Console.WriteLine(muva.PrecisionError);
            //Console.WriteLine(muva.RelativeError);
            //SimplePolynomial sis = new SimplePolynomial(new double[]{1,2,3,4,5,6,1});

            //Console.WriteLine(sis);
           
            //HalleyRootFinder rosu = new HalleyRootFinder(sis);

            //Console.WriteLine(rosu.FindRoots(1));
            //Console.WriteLine(rosu.NeededIterations);
            //Console.WriteLine(rosu.PrecisionError);

            //HardMultivariateRealFunction hry = new HardMultivariateRealFunction((__FU));


            //RealPartialDerivativeApproximator ap = new RealPartialDerivativeApproximator(hry);

            //Console.WriteLine(ap.LimitingValueApproximation(new GeneralVector( new double[]{2,-3}), 0.001));
           
            //HardRealFunction hr = new HardRealFunction(_ROF);

            //ApproximationNewtonRaphsonRootFinder anf = new ApproximationNewtonRaphsonRootFinder(hr);

            //Console.WriteLine(anf.FindRoots(-10,1e-5,1000));
            
            

            //HardComplexFunction harry = new HardComplexFunction(eCFU);

            //ComplexDerivativeApproximator lim = new ComplexDerivativeApproximator(harry);

            //Console.WriteLine(lim.LimitingValueAnalyse(new ComplexNumber(2)));

            //Console.ReadKey();




            //HardMultivariateRealFunction[] hmua = new HardMultivariateRealFunction[2];

            //hmua[0] = new HardMultivariateRealFunction(new HardMultivariateRealFunctionPointer(FU1));
            //hmua[1] = new HardMultivariateRealFunction(new HardMultivariateRealFunctionPointer(FU2));

            //NonLinearEquationSystem nes = new NonLinearEquationSystem(hmua);

            ////    NewtonNonLinearSystemSolver ns = new NewtonNonLinearSystemSolver(nes);
            //BroydenNonLinearSystemSolver ns = new BroydenNonLinearSystemSolver(nes);

            //Console.WriteLine(ns.SolveNonLinearSystem(new GeneralVector(new double[] { 1, 1 }), 1000, 1e-15));
            //Console.WriteLine(ns.NeededIterations);
            //Console.WriteLine(ns.PrecisionError);

            //GeneralVector reh = ns.SolveNonLinearSystem(new GeneralVector(new double[] { 1, 1 }), 1000, 1e-15);

            //Console.WriteLine(ns.EquationSystem.SolveAt(reh));

            //Console.ReadKey();




           // //*****************************


           // HardMultivariateComplexFunction[] chmua = new HardMultivariateComplexFunction[2];

           // chmua[0] = new HardMultivariateComplexFunction(new HardMultivariateComplexFunctionPointer(CCFU1));
           // chmua[1] = new HardMultivariateComplexFunction(new HardMultivariateComplexFunctionPointer(CCFU2));

           // ComplexNonLinearEquationSystem cnes = new ComplexNonLinearEquationSystem(chmua);

           // //   NewtonNonLinearSystemSolver ns = new NewtonNonLinearSystemSolver(nes);
           //  NewtonComplexNonLinearSystemSolver cns = new NewtonComplexNonLinearSystemSolver(cnes);

           //// BroydenComplexNonLinearSystemSolver cns = new BroydenComplexNonLinearSystemSolver(cnes);

           // Console.WriteLine(cns.SolveNonLinearSystem(new GeneralComplexVector(new ComplexNumber[] { new ComplexNumber(1), new ComplexNumber(1) }), 1000, 1e-15));
           // Console.WriteLine(cns.NeededIterations);
           // Console.WriteLine(cns.PrecisionError);


           // Console.ReadKey();







            //Console.ReadKey();
            //double[] xD = new double[5];

            //xD[0] = 4;
            //xD[1] = 49;
            //xD[2] = 22;
            //xD[3] = 66;
            //xD[4] = 5;

            //Polynomial xP = new Polynomial(xD);

            //Console.WriteLine(xP);
            //HardRealFunction hry = new HardRealFunction(new HardRealFunctionPointer(HFU));


            //SteffensenRootFinder nop = new SteffensenRootFinder(hry);

            //Console.WriteLine("ROOT " + nop.FindRoots(1,1e-15,10000));
            //Console.WriteLine("NEEDED_ITS " + nop.NeededIterations);
            //Console.WriteLine("PRE_ERR " + nop.PrecisionError);
            //Console.WriteLine("REL_ERR " + nop.RelativeError);


            //Console.WriteLine("**********************************" );

            //SimplePolynomial muah = new SimplePolynomial(1,2,3);

            //NewtonOptimizer nop = new NewtonOptimizer(muah.ToPolynomial());


            //Console.WriteLine(nop.FindMinimum(100,1000));
            //Console.WriteLine("NEEDED_ITS " + nop.NeededIterations);
            //Console.WriteLine("PRE_ERR " + nop.PrecisionError);
            //Console.WriteLine("REL_ERR " + nop.RelativeError);

           //double qe =MathOperators.ProductOperator(1,10,delegate(int i)
           //                                    {
           //                                        return  i; 
           //                                    });

           // Console.WriteLine(qe);
            //TreeNode<double> tr = new TreeNode<double>(5);

            //tr.Children.Add(new TreeNode<double>(1));
            //tr.Children.Add(new TreeNode<double>(2)); 
            //tr.Children.Add(new TreeNode<double>(3));

            double[,] data = new double[3, 3];

            data[0, 0] = 1;
            data[0, 1] = 2;
            data[0, 2] = 3;

            data[1, 0] = 4;
            data[1, 1] = 5;
            data[1, 2] = 9;

            data[2, 0] = 5;
            data[2, 1] = 2;
            data[2, 2] = 3;


  // -8  -3  -2
 // -9   2  -4
//   6  -7   4

            data[0, 0] = -8;
            data[0, 1] = -3;
            data[0, 2] = -1;

            data[1, 0] = -9;
            data[1, 1] = -2;
            data[1, 2] = -4;

            data[2, 0] = -6;
            data[2, 1] = -7;
            data[2, 2] = -4;

            //SquareMatrix m = new SquareMatrix(data);

            //Console.WriteLine(m);
            //Console.ReadKey();
            //PowerIteration pi = new PowerIteration(m);

            //Console.WriteLine(pi.FindEigenValues(new GeneralVector(new double[] { 1, 2, 3 }), 1000, 1e-15));



          //  GeneralVector tt = new GeneralVector(new double[]{1,2,3,4});

          //  Console.WriteLine(tt);

          //  tt.Normalize();

          //  Console.WriteLine(tt);

          //  Console.WriteLine("len " + tt.GetVectorLength());
          ////  double[] nud = new double[5];

          //  nud[0] = 100;
          //    nud[1] = 50;
          //    nud[2] = -9;
          //    nud[3] = 0.5;
          //    nud[4] = 5;

          //  Polynomial ppp = new SimplePolynomial(nud).ToPolynomial();
          //  SimpsonCompositeIntegrator rom = new SimpsonCompositeIntegrator(0, 5, ppp);

          ////  TrapezoidIntegrator rom = new TrapezoidIntegrator(0, 5, ppp);

          //  Console.WriteLine(ppp);
          //  Console.WriteLine(rom.Integrate(880, 0.0015));
          //  Console.WriteLine(rom.RelativeError);
          //  Console.WriteLine(rom.NeededIterations);


           // Console.WriteLine(rom.Integrate());

            //SimplePolynomial nu = new SimplePolynomial(nud);

            
            //SimplePolynomial denu = new SimplePolynomial(0,  1);

            //SimpleRationalPolynomial srp = new SimpleRationalPolynomial(nu,denu);

            //Console.WriteLine(srp);

            //Console.WriteLine("DE");

            //Console.WriteLine(srp.Derivative());

            //Console.WriteLine("IN");

            //SimpleRationalPolynomial ii = srp + srp;

            //Console.WriteLine(ii);
            //Console.ReadKey();
            //ComplexPolynomialComponent[] cpd = new ComplexPolynomialComponent[3];

            //cpd[0] = new ComplexPolynomialComponent(new ComplexNumber(1, 2), 0);
            //cpd[1] = new ComplexPolynomialComponent(new ComplexNumber(5, 12), 1);
            //cpd[2] = new ComplexPolynomialComponent(new ComplexNumber(10, 22), 2);


            //ComplexPolynomial cp = new ComplexPolynomial(cpd);

            //ComplexPolynomialSampler cs = new ComplexPolynomialSampler(cp);

            //ComplexPoint2D[] cr = cs.SamplePolynomial(new ComplexNumber(1), new ComplexNumber(10), new ComplexNumber(1, 1));

            //foreach (ComplexPoint2D wwww in cr)
            //    Console.WriteLine(wwww);
            //Console.ReadKey();
            //ComplexNumber c1 = new ComplexNumber(-2.52688172043011, -15.2043010752688);

            //ComplexNumber xa = new ComplexNumber(1,2);

            //xa.Pow(2);

            //Console.WriteLine(c1*xa);
            //Console.ReadKey();
 //           double[,] v1 = new double[3, 3];

 //           v1[0, 0] = 1;
 //           v1[0, 1] = 0;//5;
 //           v1[0, 2] = 4;

 //           v1[1, 0] = -2;
 //           v1[1, 1] = 0;// 1;
 //           v1[1, 2] = 0;// 3;

 //           v1[2, 0] = 0;//2;
 //           v1[2, 1] = 0;
 //           v1[2, 2] = -2;


 ////  3   4 -10
 ////  8   8   6
 ////-10   7   1

 //           double[,] v = new double[4, 4];

 //           v[0, 0] = 4;
 //           v[0, 1] = -30;
 //           v[0, 2] = 60;
 //           v[0, 3] = -35;

 //           v[1, 0] = -30;
 //           v[1, 1] = 300;
 //           v[1, 2] = -675;
 //           v[1, 3] = 420;

 //           v[2, 0] = 60;
 //           v[2, 1] = -675;
 //           v[2, 2] = 1620;
 //           v[2, 3] = -1050;

 //           v[3, 0] = -35;
 //           v[3, 1] = 420;
 //           v[3, 2] = -1050000;
 //           v[3, 3] = 700;

 //           SquareMatrix sm = new SquareMatrix(v1);

 //           Console.WriteLine(sm);

 //           Matrix xxx = sm.Copy();

 //           GivensQRDecomposition eigen = new GivensQRDecomposition(sm);

 //           GivensRotationMatrix[] re = eigen.ExecuteDecomposition(1e-20, 100);

 //           foreach (GivensRotationMatrix d in re)
 //               xxx *= d;

 //           Console.WriteLine(xxx);


 //           double[] no = new double[12];

 //           no[0] = -10;
 //           no[1] = 2;
 //           no[2] = 3;
 //           no[3] = 4;
 //           no[4] = 5;
 //           no[5] = 6;

 //           no[6] = 7;
 //           no[7] = 8;
 //           no[8] = 9;
 //           no[9] = 10;
 //           no[10] = 11;
 //           no[11] = 12;

            ComplexNumber[] xvals = new ComplexNumber[2];
            ComplexNumber[] yvals = new ComplexNumber[2];

            xvals[0] = new ComplexNumber(1,2);
            xvals[1] = new ComplexNumber(3, 5);
           // xvals[2] = new ComplexNumber(7, 9);

            yvals[0] = new ComplexNumber(10, 21);
            yvals[1] = new ComplexNumber(93, 9);
           // yvals[2] = new ComplexNumber(76, 49);

            //ComplexPolynomialInterpolation cominter = new ComplexPolynomialInterpolation(xvals,yvals);

            ComplexPolynomialInterpolation cominter = new ComplexPolynomialInterpolation(xvals, yvals);
            ComplexPolynomial hehe = cominter.InterpolatePolynomial();

            Console.WriteLine(hehe);
            Console.WriteLine("**************");
          //  Console.WriteLine(cominter.SolveAt(xvals[0]) + " @ " + xvals[0]);
            Console.WriteLine(hehe.SolveAt(xvals[0]) + " @ " + xvals[0]);

            Console.WriteLine("**************");
          //  Console.WriteLine(cominter.SolveAt(new ComplexNumber(2, 2)) + " @ " + xvals[0]);
            Console.WriteLine(hehe.SolveAt(new ComplexNumber(2, 2)) + " @ " + xvals[0]);
            Console.WriteLine("**************");
          //  Console.WriteLine(cominter.SolveAt(xvals[1]) + " @ " + xvals[1]);
            Console.WriteLine(hehe.SolveAt(xvals[1]) + " @ " + xvals[1]);

            Console.WriteLine("**************");
           // ComplexNumber[,] cd = new ComplexNumber[2,2];

           // cd[0,0]=new ComplexNumber(1,2);
           // cd[0, 1] = new ComplexNumber(5, 6);

           // cd[1, 0] = new ComplexNumber(-3, 4);
           // cd[1, 1] = new ComplexNumber(7, 8);

           // ComplexMatrix cm = new ComplexMatrix(cd);

           // GeneralComplexVector gcv = new GeneralComplexVector(2);

           // gcv[0] = new ComplexNumber(167,9);
           // gcv[1] = new ComplexNumber(199, 73);


           // GeneralComplexVector itaR = new GeneralComplexVector(2);

           // itaR[0] = new ComplexNumber(1, 5);
           // itaR[1] = new ComplexNumber(4, 99);


           // ComplexLinearEquationSystem ces = new ComplexLinearEquationSystem(cm,gcv);

           // GaussSeidelComplexLinearSystemSolver gseiCom = new GaussSeidelComplexLinearSystemSolver(ces);



           // Console.WriteLine(gseiCom.SolveLinearSystem());
           //Console.WriteLine(gseiCom.PrecisionError);

            //ComplexToRealLinearSystemTransform transformer = new ComplexToRealLinearSystemTransform(ces);

            //LinearEquationSystem tansres = transformer.TransformToRealEquationSystem();

            //Console.WriteLine("cm");
            //Console.WriteLine(tansres.CoefficientMatrix);

            //Console.WriteLine("vec");
            //Console.WriteLine(tansres.ResultVector);

            //Console.WriteLine("RESU");
            //GaussLinearSystemSolver gaussLinearSystemSolver = new GaussLinearSystemSolver(tansres.CoefficientMatrix, tansres.ResultVector);

            //Console.WriteLine(gaussLinearSystemSolver.SolveLinearSystem());

          //   SimplePolynomial _w =  new SimplePolynomial(no);
          //   Console.WriteLine(_w);

          //  PolynomialFactorizer fac = new PolynomialFactorizer(_w);

          //  SimplePolynomial[] __a = fac.FactorizePolynomial();

          //  Console.WriteLine("************************************************");
          //  Console.WriteLine("RESULTS");
        
          //  SimplePolynomial ende = __a[0];

          // Console.WriteLine(ende);

          //  for (int i = 1; i < __a.Length; i++)
          //  {
          //      ende *= __a[i];
          //        Console.WriteLine(__a[i]);
          //  }
         
          ////  Console.WriteLine(ende);

          //  Console.WriteLine("TEST");

          //  for (int i = -1000; i < 1000; i++ )
               // Console.WriteLine(ende.SolveAt(i) == _w.SolveAt(1));
           // Console.WriteLine(_w.SolveAt(1));

            //_w.Normalize();
            //Console.WriteLine(_w);
            //ComplexNumber[] _res = ExtendedMath.SolveQuadraticEquationComplex(_w.ValueAtRank(1), _w.ValueAtRank(0));
 
            //foreach (ComplexNumber c in _res)
            //    Console.WriteLine(c);
           

          //BairstowComplexRootFinder rr = new BairstowComplexRootFinder(_w);

          //  ComplexNumber[] _res = rr.FindRoots();

          //  foreach (ComplexNumber c in _res)
          //      Console.WriteLine(c);
           
            
            //Matrix test = new Matrix(v1);

            //GivensRotationMatrix gv = GivensRotationMatrix.CreateFromMatrix(1, 0, test);

            //Console.WriteLine(gv);
            //test *= gv;

            // GivensRotationMatrix gv1 = GivensRotationMatrix.CreateFromMatrix(2, 0, test);

            // test *= gv1;

            //Console.WriteLine(test);

          
            
            //PolynomialConstructor constr = new PolynomialConstructor(new double[]{5,3},new Point2D(1,4) );

            //Console.WriteLine(constr.ConstructPolynomial());

            //Console.WriteLine("**********************************");
            
            
            //  double[] w  = new double[5];
          //  for (int i = 0; i < 5; i++)
          //  {
          //      w[i] = i + 1;
          //  }

          //     SimplePolynomial p = new SimplePolynomial(new double[]{-10,1,2});
          //// SimplePolynomial p = new SimplePolynomial(w);
          //  RealExtremaFinder finder = new RealExtremaFinder(p);

          //  Console.WriteLine(p);
          //  RealExtrema[] x = finder.FindExtremas();

          //  foreach (RealExtrema e in x)
          //  {
          //      Console.WriteLine(e);
          //      Console.WriteLine(e.Type);
          //  }
          //  //PolynomialSampler sam = new PolynomialSampler(p);

          //  //Point2D[] re = sam.SamplePolynomial(-1, 1, 0.5);

            //foreach (Point2D qq in re)
            //    Console.WriteLine(qq);


          //double[,] data = new double[3, 3];
          //double[,] data1 = new double[2, 3];
            ////__________________________________________________________________
          //data[0, 0] = 25;
          //data[1, 0] = 64;
          //data[2, 0] = 144;

          //data[0, 1] = 5;
          //data[1, 1] = 8;
          //data[2, 1] = 12;

          //data[0, 2] = 1;
          //data[1, 2] = 1;
          //data[2, 2] = 1;


            //data[0, 3] = 106.8;
            //data[1, 3] = 177.2;
            //data[2, 3] = 279.2;


          //data[0, 0] = 12;
          //data[1, 0] = 1;
          //data[2, 0] = 3;

          //data[0, 1] = 3;
          //data[1, 1] = 5;
          //data[2, 1] = 7;

          //data[0, 2] = -5;
          //data[1, 2] = 3;
          //data[2, 2] = 13;

          //data[0, 3] = 1;
          //data[1, 3] = 28;
          //data[2, 3] = 76;

        //  data[0, 0] = 3;
        //  data[1, 0] = 5;
        // // data[2, 0] = 1;

        //  data[0, 1] = 2;
        //  data[1, 1] = -1;
        // // data[2, 1] = 7;


        //  data[0, 2] = -1;
        //  data[1, 2] = 0;
        ////  data[2, 2] = -2;





          //data[0, 3] = -1;
          //data[1, 3] = 11;
          //data[2, 3] = -15;

      GeneralVector resVec = new GeneralVector(new double[] { 1, 28, 76 });

          //data[0, 0] = 0;
          //data[1, 0] = 2;
          //data[2, 0] = 2;

          //data[0, 1] = 2;
          //data[1, 1] = -1;
          //data[2, 1] = -1;

          //data[0, 2] = -1;
          //data[1, 2] = 1;
          //data[2, 2] = -3;


           //LinearEquationSystem sys = new LinearEquationSystem(new Matrix(data), resVec);

           //GaussLinearSystemSolver gau = new GaussLinearSystemSolver(new Matrix(data), resVec);

           // Console.WriteLine(gau.SolveLinearSystem());

          //Matrix a = new Matrix(data);
          //Matrix b = new Matrix(data1);

            //Console.WriteLine(a);

            //Console.WriteLine("_______________________");

            //Console.WriteLine(a.Transpose());
        //  InverseMatrix res = InverseMatrix.FromMatrix(a);

      
            
          
         
         // res.SolveLinearSystem();
          //Console.WriteLine(res);
          //Console.WriteLine(a);
          //Console.WriteLine(a - res);
            ////data[0, 3] = 1;
            ////data[1, 3] = 28;
            ////data[2, 3] = 76;

            //Matrix m = new Matrix(data);

            ////            GaussSeidelLinearSystemSolver solver = new GaussSeidelLinearSystemSolver(m);
            //GaussSeidelLinearSystemSolver solver = new GaussSeidelLinearSystemSolver(m, new GeneralVector(new double[] { 1, 28, 76 }));


            //Console.WriteLine(solver.SolveLinearSystem(new GeneralVector(new double[] { 0, 0, 0 }), 100, 1e-15));

            ////   Console.WriteLine(solver.SolveLinearSystem());
            //Console.WriteLine(solver.CheckConverge());

            ////__________________________________________________________________


          //double[] dataq = new double[] { 1, 2, 3, 4, 5, 6 };
          //SimplePolynomial pq = new SimplePolynomial(dataq);

          //Console.WriteLine("P: " + pq);

          //NewtonRaphsonRootFinder finderq = new NewtonRaphsonRootFinder(pq);

          //Console.WriteLine(finderq.FindRoots(-10, 1e-5, 10000));
          //Console.WriteLine(finderq.PrecisionError);
            ////Console.WriteLine(p >> 1);
            ////Console.WriteLine("P: " + p);
            //Console.WriteLine(p >> 2);
            //Console.WriteLine("P: " + p);
            //Console.WriteLine(p >> 3);
            //Console.WriteLine("P: " + p);

            //UInt1024 big1 = new UInt1024(154);
            //UInt1024 big2 = new UInt1024(54);

            //UInt1024 result = big1 + big2;// +new UInt1024(3);

            ////Console.WriteLine(BitOperations.IsBitSet(3, 2));
            //Console.WriteLine("jO");





            ///***********************************************
        ////    Console.WriteLine(SpecialFunctions.GammaFunction(0.5));
     //TrigonometricPolynomial poly = new TrigonometricPolynomial();

     //     poly.A0 = -100;
     //     poly.AddComponent(new TrigonometricPolynomialComponent(1, 0.5));
     //     poly.AddComponent(new TrigonometricPolynomialComponent(-0.8, 0.007));
     //     poly.AddComponent(new TrigonometricPolynomialComponent(-0.1, 0.0));
     //     poly.AddComponent(new TrigonometricPolynomialComponent(0, 0.1));
     //     poly.AddComponent(new TrigonometricPolynomialComponent(0.3, -0.7));

     //     TrigonometricPolynomialSampler sampler = new TrigonometricPolynomialSampler(poly);

     //     Point2D[] result = sampler.SamplePolynomial(50);

     //     double[] data11 = new double[result.Length];

     //     for (int i = 0; i < result.Length; i++)
     //     {
     //         data11[i] = result[i].Y;
     //     }

     //     foreach (Point2D p in result)
     //         Console.WriteLine(p);

     //     GeneralVector gv = new GeneralVector(data11);


        //  TrigonometricPolynomialDFTInterpolation interp = new TrigonometricPolynomialDFTInterpolation(gv);


          //   Console.WriteLine(interp.InterpolateTrigonometricPolynomial(10,10));

          //TrigonometricPolynomial test = interp.InterpolateTrigonometricPolynomial();

          //for (int i = 0; i < result.Length; i++)
          //{
          //    Console.WriteLine(i + "--> " + (test.SolveAt(result[i].X) - result[i].Y));
          //}


            //HighPrecisionTimer timer = new HighPrecisionTimer(100);

                //timer.TimerTickEvent += new HighPrecisionTimerTick(timer_TimerTickEvent);

                //timer.Start();
                //timer.Start();


                //HarmonicNumber series = new HarmonicNumber();

                //Console.WriteLine(series.HarmonicNumberAtAsFraction(1));
                //Console.WriteLine(series.HarmonicNumberAtAsFraction(2));
                //Console.WriteLine(series.HarmonicNumberAtAsFraction(3));
                //Console.WriteLine(series.HarmonicNumberAtAsFraction(4));

            //Int64 q = 0;

            //while (q < 15)
            //{
            //    //Console.WriteLine(q + "  --> " + (SpecialFunctions.GeneralizedHypergeometricFunction(new double[] { 1, 1 }, new double[] { 3, 3,3 }, 2,200)));
            //    //Console.WriteLine(q + "  --> " + (SpecialFunctions.DivisorFunction(140,3)));
                
            //   // Console.WriteLine(q + "  --> " + (SpecialFunctions.TauFunction(q)));
            //    q += 1;
            //}

            //Console.WriteLine("1 --> " + SpecialFunctions.BesselFunction(5, 1, 5));
            //Console.WriteLine("2 --> " + SpecialFunctions.BesselFunction(5, 2, 5));


                //------------------------------------------------------
                //  SimplePolynomial p = new SimplePolynomial(-1, 2, 3);
                // // SimplePolynomial p = new SimplePolynomial(new double[] { 6, 11, -33, -33, 11, 6 });
                ////  SimplePolynomial p1 = new SimplePolynomial(new double[] { 1, 2, 3, 4 });

                //  //Polynomial q = p.ToPolynomial();

                //  Console.WriteLine("p " + p);

                //  NewtonRaphsonRootFinder finder = new NewtonRaphsonRootFinder(p);

                //  Console.WriteLine(finder.FindRoots(0,1000));
                //Console.WriteLine("q " +q);
                //   Console.WriteLine(p1);

                //------------------------------------------------------


                // BairstowComplexRootFinder finder = new BairstowComplexRootFinder(p);

                //ComplexNumber[] re = finder.FindRoots();

                // foreach (ComplexNumber c in re)
                // {
                //     Console.WriteLine(c);
                // }

                // Console.WriteLine(finder.PrecisionError);

                //------------------------------------------------------


                //PolynomialInterceptPointFinder interceptPointFinder = new PolynomialInterceptPointFinder(p,p1);

                //ComplexNumber[] res = interceptPointFinder.FindIntercectionPoints();

                //foreach (ComplexNumber c in res)
                //{
                //    Console.WriteLine(c);
                //}

                //------------------------------------------------------


                //Console.WriteLine( " --> " + SpecialFunctions.TauFunction(5));

                //SigmaFunctionGenerator sig = new SigmaFunctionGenerator(1);

                //int i = 0;

                //while (i < 10)
                //{
                //    Console.WriteLine(i+" --> " + sig.NextValue());
                //    Console.WriteLine(i + " --> " + SpecialFunctions.DivisorFunction(i,1) );
                //    i++;
                //}


                //int i = 0;

                //while (i < 10)
                //{
                //    Console.WriteLine(i + " --> " + SpecialFunctions.TotientFunction(i));
                //    i ++;
                //}

          //LehmanNumberFactorizer factor = new LehmanNumberFactorizer(999999999);

          //UInt64[] re = factor.Factorize();

          //foreach (long l in re)
          //    Console.WriteLine(l);

      //double[] xv = new double[5];
      //double[] yv = new double[5];

      //xv[0] = -1.5;
      //xv[1] = -0.75;
      //xv[2] = 0;
      //xv[4] = 1.5;
      //xv[3] = 0.75;


      //yv[0] = -14.1014;
      //yv[1] = -0.931596;
      //yv[2] = 0;
      //yv[4] = 14.1014;
      //yv[3] = 0.931596;


      //double[] xv = new double[2];
      //double[] yv = new double[2];

      //xv[0] = 2;
      //xv[1] = 5;



      //yv[0] = 3;
      //yv[1] = 7;




      double[] xv = new double[2];
      double[] yv = new double[2];

      xv[0] = 1;
       xv[1] = 2;
      //xv[2] = 3;

      yv[0] = 1;
     yv[1] = 8;
      //yv[2] = 27;


      //(1, 5), (2, 7), (4, 11), (6, 15).

      //double[] xv = new double[4];
      //double[] yv = new double[4];

      //xv[0] = 1;
      //xv[1] = 2;
      //xv[2] = 4;
      //xv[3] = 6;

      //yv[0] = 5;
      //yv[1] = 7;
      //yv[2] = 11;
      //yv[3] = 15;

            PolynomialInterpolation __i = new PolynomialInterpolation(xv,yv);

            //Console.WriteLine(__i.InterpolatePolynomial());
        //    NewtonPolynomialInterpolation inter = new NewtonPolynomialInterpolation(xv,yv);
           // NewtonPolynomial net = inter.InterpolatePolynomial();

           // NewtonPolynomial net = new NewtonPolynomial(new double[] { 1 }, new double[] { 5 },3);
            
            //LagrangePolynomial net = new LagrangePolynomial(xv,yv);
            //Console.WriteLine(net.SolveAt(1));
            //Console.WriteLine(net.ToPolynomial());

            Console.ReadKey();
        }

        static void obs_VariableChangeEvent(object sender)
        {
            Console.WriteLine("CHANGED");
        }

        static void timer_TimerTickEvent(object sender)
        {
            Console.WriteLine("hi");
            ((HighPrecisionTimer)sender).Stop();
            ((HighPrecisionTimer)sender).Start();
        }
    }
}
